

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-hexo-fluid/img/fluid.png">
  <link rel="icon" href="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/avatar.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="WSKH0929">
  <meta name="keywords" content="">
  
    <meta name="description" content="1 学习目标 了解Swagger的概念及作用 掌握在项目中集成Swagger自动生成API文档  2 Swagger简介前后端分离  前端 -&gt; 前端控制层、视图层 后端 -&gt; 后端控制层、服务层、数据访问层 前后端通过API进行交互 前后端相对独立且松耦合  产生的问题  前后端集成，前端或者后端无法做到“及时协商，尽早解决”，最终导致问题集中爆发  解决方案  首先定义schema">
<meta property="og:type" content="article">
<meta property="og:title" content="Swagger-狂神笔记">
<meta property="og:url" content="https://wskh0929.gitee.io/2023/01/01/Swagger-%E7%8B%82%E7%A5%9E%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="WSKH0929 的博客">
<meta property="og:description" content="1 学习目标 了解Swagger的概念及作用 掌握在项目中集成Swagger自动生成API文档  2 Swagger简介前后端分离  前端 -&gt; 前端控制层、视图层 后端 -&gt; 后端控制层、服务层、数据访问层 前后端通过API进行交互 前后端相对独立且松耦合  产生的问题  前后端集成，前端或者后端无法做到“及时协商，尽早解决”，最终导致问题集中爆发  解决方案  首先定义schema">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/20221231155837.png">
<meta property="article:published_time" content="2023-01-01T14:59:43.000Z">
<meta property="article:modified_time" content="2022-12-31T08:00:25.990Z">
<meta property="article:author" content="WSKH0929">
<meta property="article:tag" content="KuangShen">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Swagger">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/20221231155837.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>Swagger-狂神笔记 - WSKH0929 的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-hexo-fluid/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-hexo-fluid/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-hexo-fluid/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="//at.alicdn.com/t/c/font_3846514_kabxni94auf.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"wskh0929.gitee.io","root":"/my-hexo-fluid/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-hexo-fluid/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-hexo-fluid/js/utils.js" ></script>
  <script  src="/my-hexo-fluid/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-hexo-fluid/">
      <strong>WSKH0929 的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/archives">
                <i class="iconfont icon-books"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/categories">
                <i class="iconfont icon-th-large"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" href="javascript:;" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-book"></i>
                <span>文档</span>
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    <span>主题博客</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_51545953/article/details/128499341?spm=1001.2014.3001.5501">
                    
                    <span>配置指南</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    <span>图标用法</span>
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-hexo-fluid/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Swagger-狂神笔记"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        WSKH0929
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-01-01 22:59" pubdate>
           2023 年 01 月 01 日 , 晚上 10:59 , 星期日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          <!-- compatible with older versions-->
          8.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          <!-- compatible with older versions-->
          70 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Swagger-狂神笔记</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="1-学习目标"><a href="#1-学习目标" class="headerlink" title="1 学习目标"></a>1 学习目标</h1><ul>
<li>了解Swagger的概念及作用</li>
<li>掌握在项目中集成Swagger自动生成API文档</li>
</ul>
<h1 id="2-Swagger简介"><a href="#2-Swagger简介" class="headerlink" title="2 Swagger简介"></a>2 Swagger简介</h1><p><strong>前后端分离</strong></p>
<ul>
<li>前端 -&gt; 前端控制层、视图层</li>
<li>后端 -&gt; 后端控制层、服务层、数据访问层</li>
<li>前后端通过API进行交互</li>
<li>前后端相对独立且松耦合</li>
</ul>
<p><strong>产生的问题</strong></p>
<ul>
<li>前后端集成，前端或者后端无法做到“及时协商，尽早解决”，最终导致问题集中爆发</li>
</ul>
<p><strong>解决方案</strong></p>
<ul>
<li>首先定义schema [ 计划的提纲 ]，并实时跟踪最新的API，降低集成风险</li>
</ul>
<p><strong>Swagger</strong></p>
<ul>
<li>号称世界上最流行的API框架</li>
<li>Restful Api 文档在线自动生成器 =&gt; API 文档 与API 定义同步更新</li>
<li>直接运行，在线测试API</li>
<li>支持多种语言 （如：Java，PHP等）</li>
<li>官网：<a target="_blank" rel="noopener" href="https://swagger.io/">https://swagger.io/</a>    </li>
</ul>
<h1 id="3-SpringBoot集成Swagger"><a href="#3-SpringBoot集成Swagger" class="headerlink" title="3 SpringBoot集成Swagger"></a>3 SpringBoot集成Swagger</h1><p><strong>SpringBoot集成Swagger</strong> =&gt; <strong>springfox</strong>，两个jar包</p>
<ul>
<li><strong>Springfox-swagger2</strong></li>
<li>swagger-springmvc</li>
</ul>
<p><strong>使用Swagger</strong></p>
<ul>
<li>要求：jdk 1.8 + 否则swagger2无法运行</li>
</ul>
<p><strong>SpringBoot集成Swagger的步骤如下:</strong></p>
<p>1、新建一个SpringBoot-web项目</p>
<p>2、添加Maven依赖 （注意：2.9.2版本之前，之后的不行）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-swagger2<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-swagger-ui<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>3、编写HelloController，测试确保运行成功！</p>
<p>4、要使用Swagger，我们需要编写一个配置类-SwaggerConfig来配置 Swagger</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Configuration</span> <span class="hljs-comment">//配置类</span><br><span class="hljs-meta">@EnableSwagger2</span><span class="hljs-comment">// 开启Swagger2的自动配置</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SwaggerConfig</span> &#123;  <br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<p>5、访问测试 ：<a target="_blank" rel="noopener" href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a> ，可以看到swagger的界面；</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExMzIyMjkyNjUucG5n?x-oss-process=image/format.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<h1 id="4-Swagger的配置"><a href="#4-Swagger的配置" class="headerlink" title="4 Swagger的配置"></a>4 Swagger的配置</h1><p>1、Swagger实例Bean是Docket，所以通过配置Docket实例来配置Swaggger。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span> <span class="hljs-comment">//配置docket以配置Swagger具体参数</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>2、可以通过apiInfo()属性配置文档信息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//配置文档信息</span><br><span class="hljs-keyword">private</span> ApiInfo <span class="hljs-title function_">apiInfo</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-type">Contact</span> <span class="hljs-variable">contact</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Contact</span>(<span class="hljs-string">&quot;联系人名字&quot;</span>, <span class="hljs-string">&quot;http://xxx.xxx.com/联系人访问链接&quot;</span>, <span class="hljs-string">&quot;联系人邮箱&quot;</span>);<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ApiInfo</span>(<br>           <span class="hljs-string">&quot;Swagger学习&quot;</span>, <span class="hljs-comment">// 标题</span><br>           <span class="hljs-string">&quot;学习演示如何配置Swagger&quot;</span>, <span class="hljs-comment">// 描述</span><br>           <span class="hljs-string">&quot;v1.0&quot;</span>, <span class="hljs-comment">// 版本</span><br>           <span class="hljs-string">&quot;http://terms.service.url/组织链接&quot;</span>, <span class="hljs-comment">// 组织链接</span><br>           contact, <span class="hljs-comment">// 联系人信息</span><br>           <span class="hljs-string">&quot;Apach 2.0 许可&quot;</span>, <span class="hljs-comment">// 许可</span><br>           <span class="hljs-string">&quot;许可链接&quot;</span>, <span class="hljs-comment">// 许可连接</span><br>           <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;()<span class="hljs-comment">// 扩展</span><br>  );<br>&#125;<br></code></pre></td></tr></table></figure>

<p>3、Docket 实例关联上 apiInfo()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2).apiInfo(apiInfo());<br>&#125;<br></code></pre></td></tr></table></figure>

<p>4、重启项目，访问测试 <a target="_blank" rel="noopener" href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a> 看下效果；</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExNjE4NTExMzYucG5n?x-oss-process=image/format.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="5-配置扫描接口"><a href="#5-配置扫描接口" class="headerlink" title="5 配置扫描接口"></a>5 配置扫描接口</h1><p>1、构建Docket时通过select()方法配置怎么扫描接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2)<br>      .apiInfo(apiInfo())<br>      .select()<span class="hljs-comment">// 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口</span><br>      .apis(RequestHandlerSelectors.basePackage(<span class="hljs-string">&quot;nuc.ss.swagger.controller&quot;</span>))<br>      .build();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>2、重启项目测试，由于我们配置根据包的路径扫描接口，所以我们只能看到一个类</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExNjU4MzczOTEucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>3、除了通过包路径配置扫描接口外，还可以通过配置其他方式扫描接口，这里注释一下所有的配置方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java">basePackage(<span class="hljs-keyword">final</span> String basePackage) <span class="hljs-comment">// 根据包路径扫描接口</span><br>any() <span class="hljs-comment">// 扫描所有，项目中的所有接口都会被扫描到</span><br>none() <span class="hljs-comment">// 不扫描接口</span><br><span class="hljs-comment">// 通过方法上的注解扫描，如withMethodAnnotation(GetMapping.class)只扫描get请求</span><br>withMethodAnnotation(<span class="hljs-keyword">final</span> Class&lt;? <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Annotation</span>&gt; annotation)<br><span class="hljs-comment">// 通过类上的注解扫描，如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口</span><br>withClassAnnotation(<span class="hljs-keyword">final</span> Class&lt;? <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Annotation</span>&gt; annotation)<br></code></pre></td></tr></table></figure>

<p>4、除此之外，我们还可以配置接口扫描过滤：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2)<br>      .apiInfo(apiInfo())<br>      .select()<br>      .apis(RequestHandlerSelectors.basePackage(<span class="hljs-string">&quot;com.kuang.swagger.controller&quot;</span>))<br>       <span class="hljs-comment">// 配置如何通过path过滤,即这里只扫描请求以/ss开头的接口</span><br>      .paths(PathSelectors.ant(<span class="hljs-string">&quot;/ss/**&quot;</span>))<br>      .build();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>5、这里的可选值还有</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java">any() <span class="hljs-comment">// 任何请求都扫描</span><br>none() <span class="hljs-comment">// 任何请求都不扫描</span><br>regex(<span class="hljs-keyword">final</span> String pathRegex) <span class="hljs-comment">// 通过正则表达式控制</span><br>ant(<span class="hljs-keyword">final</span> String antPattern) <span class="hljs-comment">// 通过ant()控制</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/img_convert/694dc1129a3c16a84ae34b34ea75b6af.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<h1 id="6-配置Swagger开关"><a href="#6-配置Swagger开关" class="headerlink" title="6 配置Swagger开关"></a>6 配置Swagger开关</h1><p>1、通过enable()方法配置是否启用swagger，如果是false，swagger将不能在浏览器中访问了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2)<br>      .apiInfo(apiInfo())<br>      .enable(<span class="hljs-literal">false</span>) <span class="hljs-comment">//配置是否启用Swagger，如果是false，在浏览器将无法访问</span><br>      .select()<br>      .apis(RequestHandlerSelectors.basePackage(<span class="hljs-string">&quot;nuc.ss.swagger.controller&quot;</span>))<br>      .paths(PathSelectors.ant(<span class="hljs-string">&quot;/ss/**&quot;</span>))<br>      .build();<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTA2MTQzODEucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>2、如何动态配置当项目处于test、dev环境时显示swagger，处于prod时不显示？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">(Environment environment)</span> &#123;<br>   <span class="hljs-comment">// 设置要显示swagger的环境</span><br>   <span class="hljs-type">Profiles</span> <span class="hljs-variable">of</span> <span class="hljs-operator">=</span> Profiles.of(<span class="hljs-string">&quot;dev&quot;</span>, <span class="hljs-string">&quot;test&quot;</span>);<br>   <span class="hljs-comment">// 判断当前是否处于该环境</span><br>   <span class="hljs-comment">// 通过 enable() 接收此参数判断是否要显示</span><br>   <span class="hljs-type">boolean</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> environment.acceptsProfiles(of);<br>   <br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2)<br>      .apiInfo(apiInfo())<br>      .enable(b)<br>      .select()<br>      .apis(RequestHandlerSelectors.basePackage(<span class="hljs-string">&quot;com.kuang.swagger.controller&quot;</span>))<br>      .paths(PathSelectors.ant(<span class="hljs-string">&quot;/ss/**&quot;</span>))<br>      .build();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>3、可以在项目中增加一个dev的配置文件查看效果！</p>
<p>dev测试环境:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">server.port=<span class="hljs-number">8081</span><br></code></pre></td></tr></table></figure>

<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTMxMDk4MjYucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>项目运行结果:</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTM0MjUwOTAucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>pro测试环境:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">server.port=<span class="hljs-number">8082</span><br></code></pre></td></tr></table></figure>

<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTQ0NTU1MTAucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>项目运行结果:</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTQ1NTkyOTAucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="7-配置API分组"><a href="#7-配置API分组" class="headerlink" title="7 配置API分组"></a>7 配置API分组</h1><p><img src="https://img-blog.csdnimg.cn/img_convert/4f96f4709fde4ce4ced727080ddb1e0c.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<p>1、如果没有配置分组，默认是default。通过groupName()方法即可配置分组：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">(Environment environment)</span> &#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2).apiInfo(apiInfo())<br>      .groupName(<span class="hljs-string">&quot;狂神&quot;</span>) <span class="hljs-comment">// 配置分组</span><br>       <span class="hljs-comment">// 省略配置....</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>2、重启项目查看分组</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTUzNTQ3MTQucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>3、如何配置多个分组？配置多个分组只需要配置多个docket即可：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket1</span><span class="hljs-params">()</span>&#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2).groupName(<span class="hljs-string">&quot;group1&quot;</span>);<br>&#125;<br><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket2</span><span class="hljs-params">()</span>&#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2).groupName(<span class="hljs-string">&quot;group2&quot;</span>);<br>&#125;<br><span class="hljs-meta">@Bean</span><br><span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket3</span><span class="hljs-params">()</span>&#123;<br>   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2).groupName(<span class="hljs-string">&quot;group3&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>重启项目查看即可:</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzExOTU1NDMxMDIucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="8-实体配置"><a href="#8-实体配置" class="headerlink" title="8 实体配置"></a>8 实体配置</h1><p>1、新建一个实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiModel(&quot;用户实体&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>    <span class="hljs-meta">@ApiModelProperty(&quot;用户名&quot;)</span><br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-meta">@ApiModelProperty(&quot;密码&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getUsername</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setUsername</span><span class="hljs-params">(String username)</span> &#123;<br>        <span class="hljs-built_in">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getPassword</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> password;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPassword</span><span class="hljs-params">(String password)</span> &#123;<br>        <span class="hljs-built_in">this</span>.password = password;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>2、只要这个实体在<strong>请求接口</strong>的返回值上（即使是泛型），都能映射到实体项中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestController</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloController</span> &#123;<br><br>    <span class="hljs-comment">//   /error默认错误请求</span><br>    <span class="hljs-meta">@GetMapping(&quot;/hello&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">hello</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello&quot;</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//只要我们的接口中，返回值中存在实体类，他就会被扫描到Swagger中</span><br>    <span class="hljs-meta">@PostMapping(&quot;/user&quot;)</span><br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">user</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>3、重启查看测试</p>
<p><img src="https://img-blog.csdnimg.cn/img_convert/092b8abb670c6ae10f4219f477243b8d.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<p>注：并不是因为**@ApiModel<strong>这个注解让实体显示在这里了，而是只要出现在接口方法的返回值上的实体都会显示在这里，而@ApiModel和</strong>@ApiModelProperty**这两个注解只是为实体添加注释的。</p>
<p><strong>@ApiModel</strong>为类添加注释</p>
<p><strong>@ApiModelProperty</strong>为类属性添加注释</p>
<p><strong>总结</strong></p>
<ul>
<li>我们可以通过Swagger给一些比较难理解的接口或者属性，增加注释信息</li>
<li>接口文档实时更新</li>
<li>可以在线测试</li>
</ul>
<p>Swagger是一个优秀的工具，几乎所有大公司都有使用它</p>
<p>【注意点】：在正式发布的时候，关闭Swagger！！！</p>
<ul>
<li>出于安全考虑</li>
<li>而且节省内存</li>
</ul>
<h1 id="9-常用注解"><a href="#9-常用注解" class="headerlink" title="9 常用注解"></a>9 常用注解</h1><p>Swagger的所有注解定义在io.swagger.annotations包下</p>
<p>下面列一些经常用到的，未列举出来的可以另行查阅说明：</p>
<table>
<thead>
<tr>
<th><strong>Swagger注解</strong></th>
<th><strong>简单说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td>@Api(tags = “xxx模块说明”)</td>
<td>作用在模块类上</td>
</tr>
<tr>
<td>@ApiOperation(“xxx接口说明”)</td>
<td>作用在接口方法上</td>
</tr>
<tr>
<td>@ApiModel(“xxxPOJO说明”)</td>
<td>作用在模型类上：如VO、BO</td>
</tr>
<tr>
<td>@ApiModelProperty(value = “xxx属性说明”,hidden = true)</td>
<td>作用在类方法和属性上，hidden设置为true可以隐藏该属性</td>
</tr>
<tr>
<td>@ApiParam(“xxx参数说明”)</td>
<td>作用在参数、方法和字段上，类似@ApiModelProperty</td>
</tr>
</tbody></table>
<p>我们也可以给请求的接口配置一些注释:</p>
<ol>
<li>在HelloController控制类中的接口添加api接口注释</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestController</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloController</span> &#123;<br>    ......<br>    <span class="hljs-meta">@ApiOperation(&quot;Hello控制接口&quot;)</span><br>    <span class="hljs-meta">@GetMapping(&quot;/hello&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">hello2</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(&quot;用户名&quot;)</span> String username)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello&quot;</span> + username;<br>    &#125;<br>    <br>    <span class="hljs-meta">@ApiOperation(&quot;get测试&quot;)</span><br>    <span class="hljs-meta">@GetMapping(&quot;/get&quot;)</span><br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">hello2</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(&quot;用户&quot;)</span> User user)</span> &#123;<br>        <span class="hljs-keyword">return</span> user;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzEyMDE3NTUwMDEucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>2.进行try it out测试</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzEyMDI5NTgyNTUucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p>测试结果</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vbHpoX2dpdGVlL3NwcmluZ2Jvb3RfaW1hZ2UvcmF3L21hc3Rlci9pbWcvaW1hZ2UtMjAyMDA3MzEyMDMwMzQ3MDIucG5n?x-oss-process=image/format,png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<p><strong>总结</strong></p>
<ul>
<li><p>这样的话，可以给一些比较难理解的属性或者接口，增加一些配置信息，让人更容易阅读！</p>
</li>
<li><p>相较于传统的Postman或Curl方式测试接口，使用swagger简直就是傻瓜式操作，不需要额外说明文档(写得好本身就是文档)而且更不容易出错，只需要录入数据然后点击Execute，如果再配合自动化框架，可以说基本就不需要人为操作了。</p>
</li>
<li><p>Swagger是个优秀的工具，现在国内已经有很多的中小型互联网公司都在使用它，相较于传统的要先出Word接口文档再测试的方式，显然这样也更符合现在的快速迭代开发行情。当然了，提醒下大家在正式环境要记得关闭Swagger，一来出于安全考虑二来也可以节省运行时内存。</p>
</li>
</ul>
<h1 id="10-拓展：Swagger更换皮肤"><a href="#10-拓展：Swagger更换皮肤" class="headerlink" title="10 拓展：Swagger更换皮肤"></a>10 拓展：Swagger更换皮肤</h1><p>我们可以导入不同的包实现不同的皮肤定义：</p>
<p>1、默认的 <strong>访问 <a target="_blank" rel="noopener" href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span> <br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-swagger-ui<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/img_convert/14c77b0f4e4e61da5fb03f7e4442050c.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<p>2、bootstrap-ui <strong>访问 <a target="_blank" rel="noopener" href="http://localhost:8080/doc.html">http://localhost:8080/doc.html</a></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 引入swagger-bootstrap-ui包 /doc.html--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.github.xiaoymin<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>swagger-bootstrap-ui<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.9.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/img_convert/5c7bcac1772b601321f18b9a24261da8.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<p>3、Layui-ui <strong>访问 <a target="_blank" rel="noopener" href="http://localhost:8080/docs.html">http://localhost:8080/docs.html</a></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 引入swagger-ui-layer包 /docs.html--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.github.caspar-chen<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>swagger-ui-layer<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.1.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/img_convert/f08136815edb117d34fc8101db3e1a3f.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<p>4、mg-ui <strong>访问 <a target="_blank" rel="noopener" href="http://localhost:8080/document.html">http://localhost:8080/document.html</a></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 引入swagger-ui-layer包 /document.html--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.zyplayer<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>swagger-mg-ui<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0.6<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/img_convert/7cc548e36b94d23757154d23a5fcf330.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload></p>
<h1 id="11-学习链接"><a href="#11-学习链接" class="headerlink" title="11 学习链接"></a>11 学习链接</h1><p>参考链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44202871/article/details/115459836?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163042008916780265413002%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&amp;request_id=163042008916780265413002&amp;biz_id=0&amp;utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-115459836.first_rank_v2_pc_rank_v29&amp;utm_term=swagger%E7%8B%82%E7%A5%9E%E8%AF%B4&amp;spm=1018.2226.3001.4187">https://blog.csdn.net/weixin_44202871/article/details/115459836?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163042008916780265413002%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&amp;request_id=163042008916780265413002&amp;biz_id=0&amp;utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-115459836.first_rank_v2_pc_rank_v29&amp;utm_term=swagger%E7%8B%82%E7%A5%9E%E8%AF%B4&amp;spm=1018.2226.3001.4187</a></p>
<p>狂神视频链接：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Y441197Lw">https://www.bilibili.com/video/BV1Y441197Lw</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-hexo-fluid/categories/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/" class="category-chain-item">前后端交互</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-hexo-fluid/tags/KuangShen/">#KuangShen</a>
      
        <a href="/my-hexo-fluid/tags/Java/">#Java</a>
      
        <a href="/my-hexo-fluid/tags/Swagger/">#Swagger</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Swagger-狂神笔记</div>
      <div>https://wskh0929.gitee.io/2023/01/01/Swagger-狂神笔记/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>WSKH0929</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年1月1日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-hexo-fluid/2023/01/01/Nginx%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8-%E7%8B%82%E7%A5%9E%E7%AC%94%E8%AE%B0/" title="Nginx快速入门-狂神笔记">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Nginx快速入门-狂神笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-hexo-fluid/2023/01/01/Zookepper%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="Zookepper学习笔记">
                        <span class="hidden-mobile">Zookepper学习笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"jkBkMZcW6P8K0bQScoJqvQrX-9Nh9j0Va","appKey":"1k6toLLZa7DFnrENpqSs9Ouw","path":"window.location.pathname","placeholder":"留言仅限讨论，禁止广告等行为","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"https://jkbkmzcw.lc-cn-e1-shared.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-hexo-fluid/js/events.js" ></script>
<script  src="/my-hexo-fluid/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-hexo-fluid/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-hexo-fluid/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxingxing.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-hexo-fluid/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
